﻿using _2112A_2_MES._chubowen;
using _2112A_2_MES._sujiahao;
using _2112A_2_MES._wangweihao;
using _2112A_2_MES._XiangHe;
using _2112A_2_MES.XiangHes;
using _2112A_2_MES.XiangHes.WorkStations;
using FourGroups.Project;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Test.WangWeiHao;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace _2112A_2_MES.XiangHe.WorkStations
{
    [ApiExplorerSettings(GroupName = "项鹤")]
    public class WorkStationService:ApplicationService,IWorkStationService
    {
        private readonly IRepository<Workstation, int> _workrepository; //工作站
        private readonly IRepository<MdWorkshop, int> _mdshoprepository; //车间
        private readonly IRepository<ProProcess,int> _processrepository; //工序
        public WorkStationService(IRepository<Workstation, int> workrepository, IRepository<MdWorkshop, int> mdshoprepository, IRepository<ProProcess,int> processrepository)
        {
            _workrepository = workrepository;
            _mdshoprepository = mdshoprepository;
            _processrepository = processrepository;
        }
        /// <summary>
        /// 获取工作站列表
        /// </summary>
        /// <param name="workCode"></param>
        /// <param name="wordName"></param>
        /// <param name="carId"></param>
        /// <param name="proceId"></param>
        /// <returns></returns>
        [HttpGet, Route("/XiangHe/GetWorkStation")]
        public async Task<ReturnInfo<List<WorkStationDto>>> GetWorkStation(int pageIndex, int pageSize, string? workCode, string? wordName, int carId, int proceId)
        {
            var workList = from a in await _workrepository.GetListAsync()
                       join b in await _mdshoprepository.GetListAsync()
                       on a.WorkshopId equals b.Id
                       join c in await _processrepository.GetListAsync()
                       on a.ProcessId equals c.Id
                       select new WorkStationDto
                       {
                           WorkId = a.Id,
                           WorkstationAddress = a.WorkstationAddress,
                           WorkstationCode = a.WorkstationCode,
                           WorkstationName = a.WorkstationName,
                           WorkshopId = a.WorkshopId,
                           WorkshopCode = b.WorkshopCode,
                           WorkshopName = b.WorkshopName,
                           ProcessId = a.ProcessId,
                           ProcessCode = c.ProcessCode,
                           ProcessName = c.ProcessName,
                           EnableFlag = a.EnableFlag,
                           Remark = a.Remark,
                           CreatedBy = a.CreatedBy,
                           CreatedTime = a.CreatedTime,
                           UpdatedBy = a.UpdatedBy,
                           UpdatedTime = a.UpdatedTime
                       };
            if (!string.IsNullOrEmpty(workCode))
            {
                workList = workList.Where(m => m.WorkstationCode.Contains(workCode));
            }
            if (!string.IsNullOrEmpty(wordName))
            {
                workList = workList.Where(m => m.WorkstationName.Contains(wordName));
            }
            if (carId != 0)
            {
                workList = workList.Where(m => m.WorkshopId == carId);
            }
            if (proceId != 0)
            {
                workList = workList.Where(m => m.ProcessId == proceId);
            }
            var totalCount = workList.Count();
            workList = workList.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            var list = AutoMapperExt.MapToList<WorkStationDto>(workList);
            if (workList != null)
            {
                return new ReturnInfo<List<WorkStationDto>>(true,"0", list, totalCount);
            }
            return new ReturnInfo<List<WorkStationDto>>(false, "1", null, 0);
        }

        /// <summary>
        /// 所属车间
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("/XiangHe/GetWorkShopList")]
        public async Task<ReturnSelect<List<MdWorkshop>>> GetWorkShopList()
        {
            var workshopList = await _mdshoprepository.GetListAsync();
            if (workshopList != null)
            {
                return new ReturnSelect<List<MdWorkshop>>(true, "0", workshopList);
            }
            else
            {
                return new ReturnSelect<List<MdWorkshop>>(true, "1", null);
            }
        }

        /// <summary>
        /// 所属工序
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("/XiangHe/GetProcessList")]
        public async Task<ReturnInfo<List<ProProcess>>> GetProcessList()
        {
            var processList = await _processrepository.GetListAsync();
            if (processList != null)
            {
                return new ReturnInfo<List<ProProcess>>(true, "0", processList, 0);
            }
            return new ReturnInfo<List<ProProcess>>(true, "1", null,0);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="workstation"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet, Route("/XiangHe/AddWork")]
        public async Task<int> AddWork(Workstation workstation)
        {
            workstation.CreatedBy = "项鹤";
            workstation.CreatedTime = DateTime.Now;
            await _workrepository.InsertAsync(workstation);
            return 1;
        }

        /// <summary>
        /// 获取单个
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet, Route("/XiangHe/GetSingleWork")]
        public async Task<Workstation> GetSingleWork(int id)
        {
            var list = await _workrepository.GetAsync(id);
            return list;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="workstation"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet, Route("/XiangHe/UpdateWork")]
        public async Task<int> UpdateWork(Workstation workstation, int id)
        {
            var list = await _workrepository.GetAsync(id);
            list.WorkstationCode = workstation.WorkstationCode;
            list.WorkstationName = workstation.WorkstationName;
            list.WorkstationAddress = workstation.WorkstationAddress;
            list.WorkshopId = workstation.WorkshopId;
            list.ProcessId = workstation.ProcessId;
            list.EnableFlag = workstation.EnableFlag;
            list.Remark = workstation.Remark;
            list.UpdatedBy = "项鹤";
            list.UpdatedTime = DateTime.Now;
            await _workrepository.UpdateAsync(list);
            return 1;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet, Route("/XiangHe/DeleteWork")]
        public async Task<int> DeleteWork(int id)
        {
            await _workrepository.DeleteAsync(id);
            return 1;
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet,Route("/XiangHe/DeleteRangeWork")]
        public async Task<int> DeleteRangeWork(string id)
        {
            var array = id.Split(',');
            var list = await _workrepository.GetListAsync();
            var input = list.Where(m => array.Contains(m.Id.ToString()));
            await _workrepository.DeleteManyAsync(input);
            return 1;
        }
    }
}
